package com.handsome.单例模式;

/**
 * 饿汉式（静态代码块）
 * 优缺点与01相同
 * @author niechun
 * @date 2023/1/7 20:47
 */
public class SingletonHungry02 {

    public static void main(String[] args) {
        SingletonHungryTest02 instance1 = SingletonHungryTest02.getInstance();
        SingletonHungryTest02 instance2 = SingletonHungryTest02.getInstance();

        System.out.println(instance1==instance2);
        System.out.println("instance1的hashCode"+instance1.hashCode());
        System.out.println("instance2的hashCode"+instance2.hashCode());
    }
}

class SingletonHungryTest02 {
    //私有化构造方法
    private SingletonHungryTest02() {
    }
    //本类内部静态变量
    private final static SingletonHungryTest02 instance ;

    static {
        //静态代码块中实例化对象
        instance=new SingletonHungryTest02();
    }
    //提供一个共有的静态方法返回对象实例
    public static SingletonHungryTest02 getInstance() {
        return instance;
    }

}